<?php
$children[] = new jsTreeChild('111');
$children[] = new jsTreeChild('222');

$children[] = new jsTreeChild('333', 
	array(
		new jsTreeChild('444',
			array(
				new jsTreeChild('666')
			)
		),
		new jsTreeChild('555')
	)
);

$node[] = new jsTreeNode('AAA', $children);
//$node[] = new jsTreeNode('BBB', $children);

$tree = new jsTree($node);
//$tree->setPlugin('checkbox');
$tree->setPlugin('hotkeys');
$tree->setPlugin('search');

$tree->setPlugin('cookie');

$tree->addMenu('alert');
$tree->setPlugin('menu');

class jsTree{
	function __construct($data){
		$this->data = $data;
		$this->setCallback('rename');
	}
	
	function setUi($name){
		switch($name){
			case 'checkbox':
				$this->ui .= 'theme_name:"checkbox"';
				break;
		}
	}
	
	function addMenu($name){
		switch($name){
			case 'alert':
				$this->menu[] = "my_act : {
											label	: 'My own action', 
											icon	: '',
											visible	: 1,
											action	: function (NODE, TREE_OBJ) { 
												alert(1); 
											},
											separator_before : true
										}";
				break;
		}
	}
	
	function setCallback($name){
		switch($name){
			case 'search':
				$this->callback[] = "
					onsearch : function (n,t) {
						t.container.find('.search').removeClass('search');
						n.addClass('search');
					}";
			break;
			case 'rename':
				$this->callback[] = "
					onrename : function (NODE, TREE_OBJ) {
						// alert(TREE_OBJ.get_text(NODE));
					}";
			break;
		}
	}
	
	function setPlugin($name, $options = null){
		switch($name){
			case 'checkbox':
				$this->setUi('checkbox');
				$this->plugins[] = 'checkbox:{}';
				break;
			case 'hotkeys':
				$this->plugins[] = 'hotkeys:{}';
				break;
			case 'cookie':
				$this->plugins[] = 'cookie : { prefix : "jstree_" }';
				break;
			case 'menu':
				$this->plugins[] = 'contextmenu : {'.implode(", ",$this->menu).'}';
				break;
			case 'search':
				$this->setCallback('search');
				break;
			case 'rename':
				$this->setCallback('rename');
				break;
		}
	}
	
	function addNode($node){
		$this->data[] = $node;
	}
	
	function __toString(){	
		$out = 	'data : { 
						type : "json",
						opts : {
							static : [
								'.json_encode($this->data).'
							]
						}
					},
				ui : {
					'.$this->ui.'
				},
				plugins : {
					'.implode(", ",$this->plugins).'
				}';
			if(count($this->callback)){
				$out .= ', callback : {
								'.implode("\n ,",$this->callback).'
							}';
			}
		return $out;
	}
	
	
	function __toStrings(){
	
		$out = 'type : "json",';
		$out .= 'opts : {';
			$out .= 'static : [';
				$out .= json_encode($this->getTree());
			$out .= ']';
		$out .= '}';
		//$out .= json_encode(array('plugins' => $this->plugins));
		//$out .= json_encode(array('ui' => $this->ui));
		return $out;
	}
}

class jsTreeNode{
	var $data;
	var $children;

	function __construct($data, $children = null){
		$this->data = $data;
		if(!empty($children) AND is_array($children)){
			$this->children = $children;
		}
	}
	
	function createChild(){
		$this->child = array(
			'data' => $this->data,
			'children' => $this->children
		);
	}
	
	function __toString(){
		json_encode($this->child);
	}
}

class jsTreeChild{
	var $data;
	var $children;

	function __construct($data, $children = null){
		$this->data = array(
			'title' => $data,
			'attributes' => array(
				'href' => 'http://www.inob.lv',
				//'class' => 'AAA',
			)
		);
		if(!empty($children) AND is_array($children)){
			$this->children = $children;
		}
	}
	
	function getChild(){
		$this->child = array(
			'data' => $this->data,
			'children' => $this->children
		);
		return $this->child;
	}
	
	function __toString(){
		json_encode($this->getChild());
	}
}

?>
<script type="text/javascript" src="jstree/lib/jquery.js"></script> 
<script type="text/javascript" src="jstree/lib/jquery.cookie.js"></script> 
<script type="text/javascript" src="jstree/lib/jquery.hotkeys.js"></script> 
<script type="text/javascript" src="jstree/lib/jquery.metadata.js"></script> 
<script type="text/javascript" src="jstree/lib/sarissa.js"></script> 
<script type="text/javascript" src="jstree/jquery.tree.js"></script> 
<script type="text/javascript" src="jstree/plugins/jquery.tree.checkbox.js"></script> 
<script type="text/javascript" src="jstree/plugins/jquery.tree.contextmenu.js"></script> 
<script type="text/javascript" src="jstree/plugins/jquery.tree.cookie.js"></script> 
<script type="text/javascript" src="jstree/plugins/jquery.tree.hotkeys.js"></script> 
<script type="text/javascript" src="jstree/plugins/jquery.tree.metadata.js"></script> 
<script type="text/javascript" src="jstree/plugins/jquery.tree.themeroller.js"></script> 
<script type="text/javascript" src="jstree/plugins/jquery.tree.xml_flat.js"></script> 
<script type="text/javascript" src="jstree/plugins/jquery.tree.xml_nested.js"></script> 
<script>
$(function () { 
	$("#basic_json_1").tree({
		<?=$tree;?>
	});
});

function simpleCreate(){
	var t = $.tree.focused(); 
	if(t.selected){
		t.create();
	}else{
		alert("Select a node first");
	}
}

</script>

Search: <input type="text" value="" onkeyup="$.tree.focused().search($(this).val())"><br />

<!--<input type="button" onclick="$.tree.focused().rename();" value="Simple rename">-->
<input type="button" onclick="simpleCreate()" value="Simple Create">


<pre>


<style>
a.search { color:green !important; }
</style>

<div id="basic_json_1"></div>

